<!DOCTYPE html>
<html lang="en">

<head>
    <meta charmap="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工厂模式</title>
</head>

<body>
    <script>
        // 要求 
        // 1. 父类是一个抽象类, 不能直接实例化
        // 2. 子类实现自身的实例方法

        function ShoeShop (name) {
            this.name = name
            this.getName = function () {
                return this.name
            }
        }

        ShoeShop.prototype = {
            constructor: ShoeShop,
            inputShoe: function () {
                console.log('进了一双鞋')
            },
            sellShoe: function () {
                var shoe = this.buildShoe()
                shoe.getPrice()
                shoe.getSize()
                console.log('卖出了一双鞋')
                return shoe
            },
            buildShoe: function () {
                throw new Error('父类不能直接实例,需要子类来创建实例(商店不生产鞋子)')
            }
        }

        // 实现继承的几种方式 原型链继承 借用构造函数继承 组合继承 寄生组合继承
        function extend (Sup, Sub) {
            var F = function () { }
            F.prototype = Sub.prototype
            Sub.prototype = new F()
            Sub.prototype.constructor = Sub
        }

        function ChildShoeShop (name) {
            ShoeShop.call(this, 'nike')
            this.name = name
        }

        extend(ShoeShop, ChildShoeShop) // 子类继承父类

        ChildShoeShop.prototype.buildShoe = function () {
            console.log('子类生产鞋子')
            var getPrice = function () {
                console.log('获取鞋子的价格')
            }
            var getSize = function () {
                console.log('获取鞋子的尺码')
            }
            return {
                getPrice,
                getSize
            }
        }

        var shop = new ShoeShop('anta')
        console.log('shop', shop)
        console.log('shop', shop.name)
        console.log('shop', shop.getName())

        var childShop = new ChildShoeShop('匹克')
        console.log('childShop', childShop)
        console.log('childShop', childShop.name)
        console.log('childShop', childShop.getName())
        console.log('childShop', childShop.buildShoe())
        console.log('childShop', childShop.buildShoe().getPrice())
        console.log('childShop', childShop.buildShoe().getSize())

    </script>
</body>

</html>